<?php
namespace OrderAdmin\Controller;
use OrderAdmin\Model\OrderModel;
use OrderAdmin\Model\WorkerModel;
use Think\PageAdmin;

/**
 * 师傅管理控制器
 */
class StoreController extends BaseController
{

    /**
     * 初始化
     *
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();
    }

    public function detail(){
        $id = I('get.id', 0, 'intval');
        if($id === 0){
            $this->error('店铺不存在');
        }
        $data = M('store')
            ->where(['id' => $id])->find();
        if($data){
            $this->seotitle = '店铺详情';
            $this->assign(compact('data', 'id'));
            $this->display();
        }else{
            $this->error('获取信息失败');
        }
    }

    /**
     * 获取店铺的广告位信息
     */
    public function getAds(){
        $id = I('post.id', 0, 'intval');
        if($id === 0){
            ajax_return(false, '店铺不存在');
        }
        $data = M('ads')->where(['store_id'=>$id, 'status'=>'1'])->order('id asc')->select();
        foreach ($data as &$v){
            $v['full_filepath'] = makeImagePath($v['filepath']);
        }
        unset($v);
        ajax_return(true, '', $data);
    }

    /**
     * 添加门店
     */
    public function add(){
        if(IS_POST){
            $param['store_name'] = I('post.store_name', '');
            $param['headimg'] = I('post.headimg', '');
            $param['manager_name'] = I('post.manager_name', '');
            $param['mobile'] = I('post.mobile', '');
            $param['password'] = I('post.password', '');
            $param['province'] = I('post.province', '');
            $param['city'] = I('post.city', '');
            $param['area'] = I('post.area', '');
            $param['address'] = I('post.address', '');
            if($param['password']){
                $param['password'] = hash_make($param['password']);
            }

            $param['add_time'] = time();
            $param['update_time'] = time();
            $res = M('store')->add($param);
            $res ? ajax_return(true, '添加成功') : ajax_return(false, '添加失败');
        }
        $audit_user_id = I('get.audit_user_id');
        $this->assign(compact('audit_user_id'));
        $this->display();
    }

    /**
     * 修改
     */
    public function edit(){
        $id = I('get.id', 0, 'intval');
        if($id === 0){
            $this->error('店铺不存在');
        }
        $this->assign(compact('id'));
        $this->display();
    }

    public function update(){
        $id = I('post.id', 0, 'intval');
        if($id === 0){
            ajax_return(false, '店铺不存在');
        }
        $param = I('post.');
        $param['update_time'] = time();
        unset($param['imageUrl']);
        $res = M('store')->where(['id'=>$id])->save($param);
        $res ? ajax_return(true, '编辑成功') : ajax_return(false, '编辑失败');
    }


    public function getInfoById(){
        $id = I('post.id', 0, 'intval');
        if($id === 0){
            ajax_return(false, '店铺不存在');
        }
        $data = M('store')->where(['id'=>$id])->find();
        if(!$data){
            ajax_return(false, '店铺不存在');
        }
        $file = M('files')->field('filepath')->where(['file_id'=>$data['headimg']])->find();
        $data['imageUrl'] = makeImagePath($file['filepath']);
        unset($data['password']);
        ajax_return(true, '', $data);
    }
}